<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\CommentModel;
use App\Service\CommonService;
use Illuminate\Http\Request;
use Mockery\Exception;


/*
 *
 * 商品评论
 * 何家令  11.13
 */

class GoodsCommentController extends Controller
{

    //商品评论查询接口
    public function commentGet(Request $request)
    {
        $goodsId = intval($request->get('goods_id'));
        $commentList = CommentModel::where('goods_id', $goodsId)->get();
        if ($commentList == []) return CommonService::fail(9999, '查询失败', null);
        $commentData = $commentList->toArray();
        $commentData = CommonService::infiniteSort($commentData);
        return CommonService::success(200, '商品评论获取成功', $commentData);
    }

    //添加评论
    public function insertComment(Request $request)
    {
        $goodsId = intval($request->get('goods_id'));
        $pid = intval($request->get('p_id'));
        $uid = intval($request->get('uid'));
        $comment = strval($request->get('comment'));
        try {
            if (empty($goodsId) || empty($pid) || empty($uid) || empty($comment)) return CommonService::fail(9999, '系统繁忙', null);
            $res = CommentModel::create([
                'goods_id' => $goodsId,
                'p_id' => $pid,
                'uid' => $uid,
                'comment' => $comment
            ]);
            if ($res) return CommonService::success(200, '评论添加成功', null);
            return CommonService::fail(9999, '评论添加失败', null);
        } catch (Exception $exception) {
            return CommonService::fail(9999, $exception->getMessage(), null);
        }

    }
}

